﻿using System;
using DBProvider.Providers;
using DBProvider;

namespace HealthCommunity
{
	public partial class UserEdit : System.Web.UI.Page
	{
		public int UserID
		{
			get
			{
				return CommonHelper.QueryStringInt("userid");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				txtProtocal.Text = System.Configuration.ConfigurationManager.AppSettings["UserProtocal"];
				//var qqid = CommonHelper.QueryString("qqid");

				//this.txtQQID.Text = qqid;
				//this.txtEMail.Text = qqid;

				//BindDrugStore();

				BindStateProvince();

				BindCurrentUser();
			}
		}

		private void BindCurrentUser()
		{
			if(UserID > 0)
			{
				var userProvider = new UserProvider();

				var theUser = userProvider.GetUser(UserID, true);

				txtQQID.Text = theUser.QQ;
				txtEMail.Text = theUser.Email;
				//ddlDrugStore.SelectedValue = theUser.DrugStoreID.GetValueOrDefault(0).ToString();
				txtPosition.Text = theUser.Position;
				if(theUser.DrugStore != null)
				{
					ltrDrugStore.Text = theUser.DrugStore.DrugStoreName;
				}
			}
		}

		//private void BindDrugStore()
		//{
		//  var drugStoreProvider = new DrugStoreProvider();

		//  ddlDrugStore.Items.Clear();
		//  ddlDrugStore.DataSource = drugStoreProvider.GetAllDrugStore();
		//  ddlDrugStore.DataBind();
		//}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			if(UserID > 0)
			{
				var userProvider = new UserProvider();

				var theUser = userProvider.GetUser(UserID, false);

				if(theUser != null && theUser.UserID > 0)
				{
					theUser.Address = txtAddress.Text.Trim();
					theUser.Email = txtEMail.Text.Trim();
					theUser.IsAdmin = 0;
					theUser.QQ = txtQQID.Text.Trim();
					theUser.SecretAnswer1 = txtSecretAnswer1.Text.Trim();
					theUser.SecretAnswer2 = txtSecretAnswer2.Text.Trim();
					theUser.SecretQuestion1 = txtSecretQuestion1.Text.Trim();
					theUser.SecretQuestion2 = txtSecretQuestion2.Text.Trim();
					theUser.Credits = 200;
					if(rblSexFemale.Checked)
					{
						theUser.Sex = 2;
					}
					else if(rblSexMale.Checked)
					{
						theUser.Sex = 1;
					}
					else if(rblSexKeepSecret.Checked)
					{
						theUser.Sex = 0;
					}
					else
					{
						theUser.Sex = -1;
					}
					theUser.ShengFenZheng = txtSFZ.Text.Trim();
					theUser.UserCName = txtChName.Text.Trim();
					theUser.UserName = txtQQID.Text.Trim();
					theUser.UserPassword = txtPWD.Text;
					theUser.UserState = (int)UserState.Active;
					theUser.StateProvinceID = int.Parse(ddlProvince.SelectedValue);
					theUser.CityID = int.Parse(ddlCity.SelectedValue);
					theUser.LastUpdateTime = DateTime.Now;

					var selectedDrugStore = 0;
					//if(int.TryParse(ddlDrugStore.SelectedValue, out selectedDrugStore))
					//{
					//  theUser.DrugStoreID = selectedDrugStore;
					//}

					try
					{
						//var role = userProvider.GetRole("storemember");

						//if(role != null)
						//{
						//  theUser.RoleID = role.RoleID;
						//}

						if(userProvider.UpdateUser(theUser, true))
						{
							Server.Transfer("RegisterComplete.aspx?userid=" + theUser.UserID.ToString());
						}
						else
						{
							ltrMsg.Text = "操作失败";
						}
					}
					catch(Exception exc)
					{
						ltrMsg.Text = exc.Message;
					}
				}
			}
		}

		protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
		{
			var selectedItem = ddlProvince.SelectedValue;

			if(!string.IsNullOrEmpty(selectedItem))
			{
				var selectedProvinceID = int.Parse(selectedItem);

				var areaProvider = new AreaProvider();
				var cities = areaProvider.GetCityByStateProvince(selectedProvinceID);

				ddlCity.DataSource = cities;
				ddlCity.DataBind();
			}
		}

		private void BindStateProvince()
		{
			var areaProvider = new AreaProvider();

			ddlProvince.Items.Clear();
			ddlProvince.DataSource = areaProvider.GetAllStateProvince();
			ddlProvince.DataBind();
		}
	}
}